package com.dqhl.businessapp.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dqhl.businessapp.R;
import com.dqhl.businessapp.activity.LoginActivity;
import com.dqhl.businessapp.base.BaseFragment;
import com.dqhl.businessapp.listener.OnLogoutConfirmListener;
import com.dqhl.businessapp.util.DeviceUtils;
import com.dqhl.businessapp.util.UserSaveUtil;
import com.dqhl.businessapp.view.CircleImageView;
import com.dqhl.businessapp.view.LogoutConfirmDialog;

/**
 * Created by mac on 16/11/23.
 * 个人fragment
 */
public class PersonalFragment extends BaseFragment implements View.OnClickListener {

    private Context context;
    private View view;
    private TextView tv_top_title;
    private LogoutConfirmDialog logoutConfirmDialog;
    private CircleImageView iv_head;
    private LinearLayout ll_shop_setting;
    private LinearLayout ll_sales_setting;
    private TextView tv_version;
    private LinearLayout ll_logout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_personal, container, false);
        context = getActivity();

        initView(view);
        initListener();
        initData();

        return view;
    }

    private void initView(View view) {
        tv_top_title = (TextView) view.findViewById(R.id.tv_top_title);
        iv_head = (CircleImageView) view.findViewById(R.id.iv_head);
        ll_shop_setting = (LinearLayout) view.findViewById(R.id.ll_shop_setting);
        ll_sales_setting = (LinearLayout) view.findViewById(R.id.ll_sales_setting);
        tv_version = (TextView) view.findViewById(R.id.tv_version);
        ll_logout = (LinearLayout) view.findViewById(R.id.ll_logout);

    }

    private void initListener() {
        tv_top_title.setText("个 人");
        ll_shop_setting.setOnClickListener(this);
        ll_sales_setting.setOnClickListener(this);
        ll_logout.setOnClickListener(this);
    }

    private void initData() {
        String version = DeviceUtils.getVersion(context);
        tv_version.setText(version);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_shop_setting:
                toast("店铺设置");
                break;
            case R.id.ll_sales_setting:
                toast("促销设置");
                break;
            case R.id.ll_logout:
                doLogout();
                break;
        }
    }

    /**
     * 退出登陆
     */
    private void doLogout() {
        if (logoutConfirmDialog == null) {
            logoutConfirmDialog = new LogoutConfirmDialog(context, new OnLogoutConfirmListener() {
                @Override
                public void onConfirm() {
                    UserSaveUtil.saveUser(context, null);
                    forward(LoginActivity.class);
                }
            });
        }
        logoutConfirmDialog.show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (logoutConfirmDialog != null) {
            logoutConfirmDialog.cancel();
        }
    }
}
